package com.gz.logistics.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.app.ApplyclothesinfoActivity;
import com.gz.logistics.app.ClothesDetailActivity;
import com.gz.logistics.fragment.HomeFragment;
import com.gz.logistics.library.pojo.GetClothes;
import com.gz.logistics.library.pojo.WillOutStorageAassembleY;
import com.gz.logistics.library.pojo.WillOutStorageProdoctInfo;
import com.gz.logistics.library.util.CrashHandler;

import java.io.Serializable;
import java.util.List;

/**
 * 预出库单适配器
 * author wanner1991
 */
public class GetClothesOrderAdapter extends BaseAdapter{
    private String TAG = GetClothesOrderAdapter.class.getSimpleName();
    private List<GetClothes> mDataList;
    private Context mContext;
    private SignALLCallbackInterface callback;

    public GetClothesOrderAdapter(List<GetClothes> mDataList, Context mContext,SignALLCallbackInterface callback) {
        this.mDataList = mDataList;
        this.mContext = mContext;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.query_clothes_item,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_order_number = (TextView) view.findViewById(R.id.tv_order_number);
            viewHolder.tv_receiver_name = (TextView) view.findViewById(R.id.tv_receiver_name);
            viewHolder.tv_receiver_phone = (TextView) view.findViewById(R.id.tv_receiver_phone);
            viewHolder.tv_receiver_address = (TextView) view.findViewById(R.id.tv_receiver_address);
            viewHolder.tv_clothestype = (TextView) view.findViewById(R.id.tv_clothestype);
            viewHolder.tv_clothesmoney = (TextView) view.findViewById(R.id.tv_clothesmoney);
            viewHolder.tv_clothetime = (TextView) view.findViewById(R.id.tv_clothetime);
            viewHolder.btn_money = (RelativeLayout) view.findViewById(R.id.btn_money);
            viewHolder.get_money = (Button) view.findViewById(R.id.get_money);
            viewHolder.clothes_info = (LinearLayout) view.findViewById(R.id.clothes_info);
            viewHolder.sign_clothes = (Button) view.findViewById(R.id.sign_clothes);
            viewHolder.tv_real_money = (TextView) view.findViewById(R.id.tv_real_money);
            viewHolder.add_clothes = (Button) view.findViewById(R.id.add_clothes);
            viewHolder.tv_count = (TextView) view.findViewById(R.id.tv_count);

            view.setTag(viewHolder);
        }

        final  GetClothes assemble = mDataList.get(i);
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.sign_clothes.setVisibility(View.GONE);//隐藏签收按钮
        viewHolder.tv_order_number.setText("预约单号：" + assemble.getOrderno());
        viewHolder.tv_receiver_name.setText("客户姓名："+assemble.getClient());
        viewHolder.tv_receiver_phone.setText("联系方式：" + assemble.getPhone());
        viewHolder.tv_receiver_address.setText("客户地址：" + assemble.getAddress());
        viewHolder.tv_clothestype.setText("衣物类别：" + assemble.getClothgategory());
        viewHolder.tv_clothesmoney.setText("应收金额：" + assemble.getMoney());
        viewHolder.tv_clothetime.setText("预约时间：" + assemble.getGreservetime());
        viewHolder.tv_real_money.setText("实收金额："+ assemble.getRealmoney());
        if(assemble.getDetailses()==null){
            viewHolder.tv_count.setText("已收0件衣物");
        }else{
            viewHolder.tv_count.setText("已收"+assemble.getDetailses().size()+"件衣物");
        }
        if (HomeFragment.status == 1){//已经收取衣服的订单列表
            viewHolder.add_clothes.setVisibility(View.GONE);
            viewHolder.btn_money.setVisibility(View.GONE);
        }else{
            viewHolder.add_clothes.setVisibility(View.VISIBLE);
            viewHolder.btn_money.setVisibility(View.VISIBLE);
        }

        viewHolder.get_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.signAll(assemble.getOrderno(),(assemble.getMoney()-assemble.getRealmoney()),"收取");
            }
        });
        viewHolder.clothes_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (assemble.getDetailses()==null){//代表是未收衣服
                    Intent intent = new Intent(mContext, ApplyclothesinfoActivity.class);//跳转到提交衣物详细信息界面
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("order_no",assemble.getOrderno());
                    mContext.startActivity(intent);
                }else{
                    Intent intent = new Intent(mContext, ClothesDetailActivity.class);//跳转到详情页面
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("user_type",HomeFragment.user_type);
                    intent.putExtra("clotheslist",(Serializable)assemble);
                    mContext.startActivity(intent);

                }
            }
        });
        /**
         * 点击新增按钮跳转至提交衣物详细信息界面
         */
        viewHolder.add_clothes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ApplyclothesinfoActivity.class);//跳转到提交衣物详细信息界面
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("order_no",assemble.getOrderno());
                mContext.startActivity(intent);
            }
        });
        return view;

    }

    private static class  ViewHolder{
        private TextView tv_order_number;//订单编号
        private TextView tv_receiver_name;//用户姓名
        private TextView tv_receiver_phone;//用户电话
        private TextView tv_receiver_address;//用户地址
        private TextView tv_clothestype;//衣服种类
        private TextView tv_clothesmoney;//价格
        private TextView tv_clothetime;//预约时间
        private RelativeLayout btn_money;
        private Button get_money;
        private LinearLayout clothes_info;
        private Button sign_clothes;//签收按钮
        private TextView tv_real_money;//实收金额
        private Button add_clothes;//新增衣物按钮
        private TextView tv_count;//统计此预约单已收的衣物件数
    }
    public interface  SignALLCallbackInterface{
        void signAll(String order_num,double money,String style);
    }

}
